package com.easy.util;

import com.easy.bean.Order;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtil {

    // 导出入库订单列表到Excel
    public static byte[] exportInOrderListToExcel(List<Order> orders) throws IOException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

        // 统计每个 in_id 的出现次数
        Map<String, Integer> orderCountMap = new HashMap<>();
        for (Order order : orders) {
            orderCountMap.put(order.getIn_id(), orderCountMap.getOrDefault(order.getIn_id(), 0) + 1);
        }

        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("入库订单");

            // 创建表头
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("入库订单ID");
            headerRow.createCell(1).setCellValue("商品ID");
            headerRow.createCell(2).setCellValue("商品名称");
            headerRow.createCell(3).setCellValue("入库数量");
            headerRow.createCell(4).setCellValue("单位");
            headerRow.createCell(5).setCellValue("入库价格");
            headerRow.createCell(6).setCellValue("该商品入库总价");
            headerRow.createCell(7).setCellValue("入库时间");

            int rowNum = 1;
            int mergeStartRow = 1;
            String lastInId = null;

            for (int i = 0; i < orders.size(); i++) {
                Order order = orders.get(i);
                Row row = sheet.createRow(rowNum++);

                if (order.getIn_id().equals(lastInId)) {
                    if (orderCountMap.get(lastInId) > 1) {
                        // 如果订单号重复出现并且数量大于1，则跳过单元格填充
                        row.createCell(0).setCellValue("");
                    }
                } else {
                    if (i > 0 && orderCountMap.get(lastInId) > 1) {
                        // 不同的 In_id，且前一个 In_id 的数量大于1时进行合并
                        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, rowNum - 2, 0, 0));
                    }
                    // 填充新的 In_id
                    row.createCell(0).setCellValue(order.getIn_id());
                    mergeStartRow = rowNum - 1;
                    lastInId = order.getIn_id();
                }

                row.createCell(1).setCellValue(order.getProduct_id());
                row.createCell(2).setCellValue(order.getProduct_name());
                row.createCell(3).setCellValue(order.getIn_num());
                row.createCell(4).setCellValue(order.getUtil());
                row.createCell(5).setCellValue(order.getIn_price().doubleValue());
                row.createCell(6).setCellValue(order.getIn_sum_price().doubleValue());
                row.createCell(7).setCellValue(order.getIn_date().format(formatter));
            }

            // 合并最后一组相同的 In_id，如果有多个且需要合并
            if (orderCountMap.get(lastInId) > 1 && mergeStartRow < rowNum - 1) {
                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, rowNum - 1, 0, 0));
            }

            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                workbook.write(baos);
                return baos.toByteArray();
            }
        }
    }

    // 导出出库订单列表到Excel
    public static byte[] exportOutOrderListToExcel(List<Order> orders) throws IOException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

        // 统计每个 out_id 的出现次数
        Map<String, Integer> orderCountMap = new HashMap<>();
        for (Order order : orders) {
            orderCountMap.put(order.getOut_id(), orderCountMap.getOrDefault(order.getOut_id(), 0) + 1);
        }

        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("出库订单");

            // 创建表头
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("出库订单ID");
            headerRow.createCell(1).setCellValue("商品ID");
            headerRow.createCell(2).setCellValue("商品名称");
            headerRow.createCell(3).setCellValue("出库数量");
            headerRow.createCell(4).setCellValue("单位");
            headerRow.createCell(5).setCellValue("出库价格");
            headerRow.createCell(6).setCellValue("该商品出库总价");
            headerRow.createCell(7).setCellValue("出库时间");

            int rowNum = 1;
            int mergeStartRow = 1;
            String lastOutId = null;

            for (int i = 0; i < orders.size(); i++) {
                Order order = orders.get(i);
                Row row = sheet.createRow(rowNum++);

                if (order.getOut_id().equals(lastOutId)) {
                    if (orderCountMap.get(lastOutId) > 1) {
                        // 如果订单号重复出现并且数量大于1，则跳过单元格填充
                        row.createCell(0).setCellValue("");
                    }
                } else {
                    if (i > 0 && orderCountMap.get(lastOutId) > 1) {
                        // 不同的 Out_id，且前一个 Out_id 的数量大于1时进行合并
                        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, rowNum - 2, 0, 0));
                    }
                    // 填充新的 Out_id
                    row.createCell(0).setCellValue(order.getOut_id());
                    mergeStartRow = rowNum - 1;
                    lastOutId = order.getOut_id();
                }

                row.createCell(1).setCellValue(order.getProduct_id());
                row.createCell(2).setCellValue(order.getProduct_name());
                row.createCell(3).setCellValue(order.getOut_num());
                row.createCell(4).setCellValue(order.getUtil());
                row.createCell(5).setCellValue(order.getOut_price().doubleValue());
                row.createCell(6).setCellValue(order.getOut_sum_price().doubleValue());
                row.createCell(7).setCellValue(order.getOut_date().format(formatter));
            }

            // 合并最后一组相同的 Out_id，如果有多个且需要合并
            if (orderCountMap.get(lastOutId) > 1 && mergeStartRow < rowNum - 1) {
                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, rowNum - 1, 0, 0));
            }

            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                workbook.write(baos);
                return baos.toByteArray();
            }
        }
    }
}
